/*************************************************************************
    > File Name: class.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: 2022年10月01日 星期六 16时49分26秒
 ************************************************************************/

#include"buffer.h"

void Ms()
{
  for(kk1=0;kk1<1100;kk1++)
        for(kk2=0;kk2<1200;kk2++)
          for(kk3=0;kk3<1200;kk3++);
}
void keyboardc()//录入课程子函数(从键盘录入)
{
struct couse *incouse;
N1=0;
incouse=(struct couse*)malloc(sizeof(struct couse));
printf("课程编号\t课程名称\t学分\t课程人数上限\n");
scanf("%d%s%d%d",&incouse->num1,&incouse->name1,&incouse->score,&incouse->Melepeo);
incouse->nelepeo=0;
 struct couse *p0,*p1,*p2;
  p1=head1;
  p0=incouse;
  if(head1==NULL)
  {
    head1=p0;
    p0->next=NULL;
  }
  else
  {
    while((p0->num1 > p1->num1) && (p1->next!=NULL))
    {
      p2=p1;
      p1=p1->next;
    }
    if(p0->num1 <= p1->num1)
    {
      if(head1==p1) head1=p0;
      else p2->next=p0;
      p0->next=p1;
    }
    else
    {
      p1->next=p0;
      p0->next=NULL;
    }
  }
  N1=N1+1;
}

void filec()//录入键盘子函数(从文件录入)
{
  FILE * fp;
  char filepath[20]; 
  struct couse *p1,*p2;
  N1=0;
  printf("输入要读入的文件路径:");
  getchar();
  gets(filepath); 
  if((fp=fopen(filepath,"r"))==NULL) 
  { 
    printf("找不到%s文件!\n",filepath); 
    exit(0); 
  } 
  p1=p2=(struct couse*)malloc(sizeof(struct couse));
  fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo);
  head1=NULL;
  while(!feof(fp))
  {
    N1=N1+1;
    if(N1==1)head1=p1;
    else p2->next=p1;
    p2=p1;
    p1=(struct couse * )malloc(sizeof(struct couse));
    fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo);
  } 
  p2->next=NULL;
}
void inputc()//录入课程主函数
{
  int i;
  printf("\t\t\t录入课程信息\n");
  printf("\n1.从键盘录入\n");
  printf("2.从文件录入\n");
  printf("3.返回主菜单\n");
  printf("请选择(1~3):\n");
  scanf("%d",&i);
  switch(i)
  {
  case(1):keyboardc();break;
  case(2):filec();break;
  case(3):break;
  }
}
void insertc(struct couse *incouse)//课程管理子函数(增加课程)
{
  struct couse *p0,*p1,*p2;
  p1=head1;
  p0=incouse;
  if(head1==NULL)
  {
    head1=p0;
    p0->next=NULL;
  }
  else
  {
    while((p0->num1 > p1->num1) && (p1->next!=NULL))
    {
      p2=p1;
      p1=p1->next;
    }
    if(p0->num1 <= p1->num1)
    {
      if(head1==p1) head1=p0;
      else p2->next=p0;
      p0->next=p1;
    }
    else
    {
      p1->next=p0;
      p0->next=NULL;
    }
  }
  N1=N1+1;
}
void delc(int num1)//课程管理子函数(删除课程)
{
  struct couse *p1,*p2;
  if(head1==NULL)
  {
    printf("\n没有课程,无法删除!\n");
    goto end;
  }
  p1=head1;
  while(num1!=p1->num1 && p1->next!=NULL)
  {
    p2=p1;
    p1=p1->next;
  }
  if(num1==p1->num1)
  {
    if(p1==head1) head1=p1->next;
    else p2->next=p1->next;
    printf("已删除该编号课程!\n");
    N1=N1-1;
  }
  else printf("无该编号的课程!\n");
  end:;
}
void managementc()//课程管理主函数
{
  struct couse * incouse;
  int i,num1;
  printf("\t\t\t课程管理\n");
  printf("1.新增课程\n");
  printf("2.删除课程\n");
  printf("3.返回主菜单\n");
  printf("请选择(1~3):\n");
  scanf("%d",&i);
  switch(i)
  {
  case(1):
    {
      incouse=(struct couse *)malloc(sizeof(struct couse));
      printf("课程编号\t课程名称\t学分\t课程人数上限\n");
      scanf("%d%s%d%d",&incouse->num1,incouse->name1,&incouse->score,&incouse->Melepeo);
      incouse->nelepeo=0;
      insertc(incouse);
      break;
    }
  case(2):
    {
      printf("请输入要删除课程的编号:\n");
      scanf("%d",&num1);
      delc(num1);
      break;
    }
  case(3):break;
  }
}




void elect(struct student * s)//选课
{
  struct couse * p;
  int num1,i;
  printf("请输入要选课的编号:\n");
  scanf("%d",&num1);
  for(i=0;s->nelenum[i]!=0;i++);
  s->nelenum[i]=num1;
  (s->nelen)++;
  p=head1;
  while(p->num1!=num1) p=p->next;
  (p->nelepeo)++;
}
void cheak()//学生选课子函数(查询可选课程)
{
  char e;
  struct couse * c;
  struct student * s;
  int num2,i,j=0,t=0;
  printf("请输入你的学号:");
  scanf("%d",&num2);
  s=head2;
  while(s->num2!=num2 && s->next!=NULL) s=s->next;
  if(s->num2!=num2)
  {
    printf("不存在你的信息,请进入主菜单录入你的信息!\n");
    goto end;
  }
  c=head1;
  printf("你的可选课程编号:\n");
  while(c!=NULL)
  {
    for(t=0,i=0;s->nelenum[i]!=0;i++)
    {
      if(c->num1==s->nelenum[i]) t=1;
    }
    if(t==0 && (c->nelepeo!=c->Melepeo))
    {
      printf("%d\n",c->num1);
      j++;
    }
    c=c->next;
  }
  if(j==0)
  {
    printf("你已选完所有课程，无法再多选!\n");
    goto end;
  }
  printf("选课(y/n)?:\n");
  getchar();
  e=getchar();
  i=0;
  while(e=='y')
  {
    elect(s);
    printf("继续选课(y/n)?:\n");
    getchar();
    e=getchar();
  }
  end:;
}
void back(struct student * p)//退课
{
  struct couse * p1;
  int num1,i,j;
  printf("请输入你要退掉的课程编号:\n");
  scanf("%d",&num1);
  p1=head1;
  while(p1->num1!=num1) p1=p1->next;
  for(i=0;p->nelenum[i]!=num1;i++);
  for(j=i;p->nelenum[j]!=0;j++) p->nelenum[j]=p->nelenum[j+1];
  p->nelenum[--j]=0;
  (p1->nelepeo)--;
  printf("退课成功!\n");
}
void hcheak()//学生选课子函数（查询已选课程)
{
  char c;
  struct couse * p0;
  struct student * p;
  int num2,i,f=0;
  printf("请输入学号:\n");
  scanf("%d",&num2);
  p=head2;
  while(p->num2!=num2 && p!=NULL) p=p->next;
  if(p==NULL)
  {
    printf("不存在你的信息,请回主菜单录入信息:\n");
    goto end;
  }
  printf("已选课程编号:\n");
  if(p->nelenum[0]==0)
  {
    printf("你还没选课!\n");
    goto end;
  }
  for(i=0;p->nelenum[i]!=0;i++)
  {
    printf("%d\n",p->nelenum[i]);
    p0=head1;
    while(p0->num1!=p->nelenum[i]) p0=p0->next;
    f=f+p0->score;
  }
  printf("总学分:%d\n",f);
  printf("是否进行退课(y/n)?");
  getchar();
  c=getchar();
  while(c=='y')
  {
    back(p);
    printf("继续退课(y/n)?");
    getchar();
    c=getchar();
    (p->nelen)--;
  }
  end:;
}
void elective()//学生选课主函数
{
  int i;
  printf("\t\t\t学生选课\n");
  printf("1.查询可选课程\n");
  printf("2.查询已选课程\n");
  printf("3.返回主菜单\n");
  printf("请输入(1~3):\n");
  scanf("%d",&i);
  switch(i)
  {
  case(1):cheak();break;
  case(2):hcheak();break;
  case(3):break;
  }
}


